<template>
    <div class="my-target cardPage">
        <!-- 搜索 -->
        <search :key="levelType" :level-type="levelType" @search="search">
        </search>

        <!-- tabs数据 -->
        <div class="level-content">
            <cardToList
                    :tabs="tabs"
                    :card-data="cardData"
                    :level-type="levelType"
                    :checkStatus="false"
                    :isEdit="true"
                    :isDelete="true"
                    @jumpList="jumpList"
                    @del="del"
                    @edit="edit"
                    @tabClick="tabClick"
                    @changeId="changeId"
                    @changePage="changePage"></cardToList>
        </div>

        <!-- 新增、编辑、复制目标标准弹框 -->
        <add-dialog
                v-if="addParams.visible"
                :levelType="levelType"
                :addParams="addParams"
                :currentId="currentId"
                :parentData="cardData"
                @getList="getList"></add-dialog>
    </div>
</template>

<script>
    import search from '@/components/search/levelSearch';
    import cardToList from '@/components/cardToList/index';
    import addDialog from '@/views/targetSystem/targetManage/module/addLevel';
    import { queryMyTarget, delMyTarget} from "../../../api/target";
    import {getStorage} from "@/config/utils";

    export default {
        name: 'myTarget',
        components: {
            search,
            cardToList,
            addDialog
        },
        data () {
            return {
                searchFrom: {
                    name: '',
                    dutyPart: '',
                    year: '',
                    rectificationRangeId: 0,
                    pageIndex: 1,
                    pageSize: 10
                },
                tabs: [],
                // 层级：学校层，专业层，课程层，教师层，学生层
                levelType: '',
                // 新增相关参数
                addParams: {
                    visible: false,
                    title: ''
                },
                cardData: [],
                currentId: '', //当前选的id
            }
        },
        created () {
            // 年度默认为当前年份
            this.searchFrom.year = (new Date()).getFullYear();
            this.levelType = this.$route.params.type;
            this.setTabsByType();
        },
        methods: {
            // 新增、复制、新增子项目标标准弹框
            handleDialog (type) {
                switch (type) {
                    case 'add':
                        this.addParams = {
                            visible: true,
                            title: '添加目标标准',
                            type: 'add'
                        };
                        break;
                    case 'copy':
                        this.addParams = {
                            visible: true,
                            title: '复制目标标准',
                            type: 'copy'
                        };
                        break;
                    case 'child':
                        this.addParams = {
                            visible: true,
                            title: '新增子项',
                            type: 'child'
                        };
                        break;
                    case 'set':
                        break;
                }
            },
            // 根据菜单上的层级查询对应的适用范围
            setTabsByType () {
                let list = getStorage('rangeList');
                if (list && list.length) {
                    let temp = list.filter(item => item.code === this.levelType);
                    if (temp.length) {
                        this.tabs = temp[0].rectificationRangeDTOList;

                        // 默认显示第一个tabs页的数据
                        this.searchFrom.rectificationRangeId = this.tabs[0].id;
                        this.getList();
                    }
                }
            },
            // 获取列表
            getList () {
                queryMyTarget(this.searchFrom).then(res => {
                    this.cardData = res;
                }).catch()
            },
            // 查询
            search (searchFrom) {
                this.searchFrom.name = searchFrom.name;
                this.searchFrom.dutyPart = searchFrom.dutyPart;
                this.searchFrom.year = searchFrom.year;
                this.getList();
            },
            // 跳转进入列表页
            jumpList (id) {
                this.$router.push({
                    path: `/target/myTarget/levelList/${this.levelType}`,
                    query:{
                        index: 2,
                        id: id
                    }
                })
            },
            // 删除
            del (id) {
                delMyTarget(id).then(res => {
                    this.$message.success('操作成功');
                    this.getList();
                }).catch()
            },
            // 编辑
            edit (id) {
                this.$router.push({
                    path: `/target/myTarget/levelList/${this.levelType}`,
                    query: {
                        index: 1,
                        id: id
                    }
                })
            },
            // 切换tabs
            tabClick (id) {
                this.searchFrom.rectificationRangeId = +id;
                this.getList();
            },
            // 选中的卡片
            changeId (id) {
                this.currentId = id;
            },
            // 切换分页
            changePage(pageIndex) {
                this.searchFrom.pageIndex = pageIndex;
                this.getList()
            }
        },
        watch: {
            '$route' (to, from) {
                this.levelType = to.params.type;
                this.setTabsByType()
            }
        }
    }
</script>

<style scoped>

</style>